package sharetime.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

public class FileUploadUtils {

	@SuppressWarnings("unchecked")
	public static byte[] handlePictureUpload(HttpServletRequest request) {
		MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;

		List<String> fileNames = new ArrayList<String>();
		for (Iterator i = req.getFileNames(); i.hasNext();) {
			String fileName = (String) i.next();
			fileNames.add(fileName);
		}

		MultipartFile mf = req.getFile(fileNames.get(0));
		try {
			return mf.getBytes();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
